package com.klikmee.sms.chat.web.usecases;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONException;

import com.dotexe.webframework.core.ClientApplication;
import com.dotexe.webframework.core.datasources.CollectionDataSource;
import com.dotexe.webframework.core.datasources.ObjectDataSource;
import com.dotexe.webframework.core.triggers.Trigger;
import com.klikmee.sms.chat.web.dao.AgentDAO;
import com.klikmee.sms.chat.web.dao.AgentGroupDAO;
import com.klikmee.sms.chat.web.dao.GroupDAO;
import com.klikmee.sms.chat.web.util.SpringBeansLookup;
import com.klikmee.sms.services.chat.model.Agent;
import com.klikmee.sms.services.chat.model.AgentGroup;
import com.klikmee.sms.services.chat.model.Group;

public class EmployeeDetailsForm extends ClientApplication {

	private ObjectDataSource dsAgent;
	private CollectionDataSource dsGroups;
	private ArrayList<String> bcAgent;
	private ArrayList<String> bcGroups;

	private Agent agent;
	private AgentGroup ag;
	private boolean editAgent;
	private GroupDAO groupDAO;
	private List<Group> groups;
	private ArrayList<Object> agentGroups;
	private AgentGroupDAO agentsGroupDAO;

	// override
	public void load() throws Exception {
		super.load();

		this.dsAgent = new ObjectDataSource("dsAgent");
		this.getDataSources().add(this.dsAgent);
		this.dsGroups = new CollectionDataSource("dsGroups");
		this.dsGroups.setMultiselect(true);
		this.getDataSources().add(this.dsGroups);

		this.bcAgent = new ArrayList<String>();
		this.bcAgent.add("AgentFirstName");
		this.bcAgent.add("AgentLastName");
		this.bcAgent.add("AgentUserName");
		this.bcAgent.add("AgentPassword");
		this.bcAgent.add("AgentEmail");
		this.bcAgent.add("AgentPhone");
		this.bcAgent.add("Active");

		this.bcGroups = new ArrayList<String>();
		this.bcGroups.add("GroupName");
		this.bcGroups.add("GroupDescription");

		this.triggers.add(new Trigger(this, "btnSaveClick"));
		this.triggers.add(new Trigger(this, "btnCancelClick"));
		this.triggers.add(new Trigger(this, "gvGroupsCellClick"));
	}

	@Override
	public void start() throws Exception {
		super.start();

		ChangeControlCollapsible("AppEmployeeDetails_borderLayout1_borderLayout1PanelRight", true);

		this.dsAgent.setSource(agent);
		this.dsAgent.bind(bcAgent);

		bindGroupsList();
	}

	public void btnSaveClick() throws JSONException {
		AgentDAO agentDAO = SpringBeansLookup.getBean(AgentDAO.class);

		agent.setAgentFirstName(((Agent) this.dsAgent.getSource()).getAgentFirstName());
		agent.setAgentLastName(((Agent) this.dsAgent.getSource()).getAgentLastName());
		agent.setAgentUserName(((Agent) this.dsAgent.getSource()).getAgentUserName());
		agent.setAgentPassword(((Agent) this.dsAgent.getSource()).getAgentPassword());
		agent.setAgentEmail(((Agent) this.dsAgent.getSource()).getAgentEmail());
		agent.setAgentPhone(((Agent) this.dsAgent.getSource()).getAgentPhone());
		agent.setActive(((Agent) this.dsAgent.getSource()).getActive());

		if (editAgent) {
			agentDAO.edit(agent);
		} else {
			agentDAO.create(agent);
		}

		agentsGroupDAO = SpringBeansLookup.getBean(AgentGroupDAO.class);
		List<AgentGroup> check = agentsGroupDAO.findByAgent(agent);
		for (Object item : check) {
			agentsGroupDAO.delete((AgentGroup) item);
		}

		for (Object itemGroup : this.dsGroups.getSelectedItems()) {

			agentsGroupDAO = SpringBeansLookup.getBean(AgentGroupDAO.class);

			ag = new AgentGroup();
			ag.setAgent(agent);
			ag.setGroup((Group) itemGroup);

			agentsGroupDAO.create(ag);
		}

		this.hide();
	}

	public void btnCancelClick() throws JSONException {
		this.hide();
	}

	public void gvGroupsCellClick() {
		
	}

	public void setAgent(Agent agent) {
		this.agent = agent;
	}

	public Agent getAgent() {
		return agent;
	}

	public void setEditAgent(boolean editAgent) {
		this.editAgent = editAgent;
	}

	public boolean getEditAgent() {
		return editAgent;
	}

	private void bindGroupsList() throws SecurityException, ClassNotFoundException, NoSuchFieldException, IllegalAccessException, JSONException, NoSuchMethodException, InvocationTargetException {
		groupDAO = SpringBeansLookup.getBean(GroupDAO.class);
		groups = groupDAO.selectAll();
		this.dsGroups.setSource(groups);

		if (agent.getAgentID() != 0) 
		{
			agentGroups = (ArrayList<Object>) groupDAO.selectAllGroupsForAgent(agent);
			if (agentGroups != null) {
				this.dsGroups.setSelectedItems(agentGroups);
			}
		}

		this.dsGroups.bind(bcGroups);
	}
}